package com.ukao.cashregister.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ukao.cashregister.R;
import com.ukao.cashregister.listener.OnClickPopWindowListener;
import com.ukao.cashregister.utils.CheckUtils;

import java.util.Set;

public class SpinnerPopWindow extends PopupWindow {
	private Context mContext;
	private TagFlowLayout mListView;
	private TagAdapter mAdapter;
	private OnClickPopWindowListener mItemSelectListener;
	private TextView mTextView;
	private String  mMessage;
	private int selectPos;
	private int defaultPos;
	public SpinnerPopWindow(Context context)
	{
		super(context);
		mContext = context;
		init();
	}

	public SpinnerPopWindow(Context context, String  message)
	{
		super(context);
		mContext = context;
		mMessage=message;
		init();
	}


	public void setItemListener(OnClickPopWindowListener listener){
		mItemSelectListener = listener;
	}

	public void setAdapter(TagAdapter adapter, int defaultPos){
		mAdapter = adapter;
		mListView.setAdapter(mAdapter);
		mAdapter.setSelectedList(defaultPos);
		mListView.setOnSelectListener(mOnSelectListener);

	}


	private void init()
	{
		View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_order_tag, null);
		setContentView(view);
		setWidth(LayoutParams.WRAP_CONTENT);
		setHeight(LayoutParams.MATCH_PARENT);
		setTouchable(true);
		setFocusable(true);
		ColorDrawable dw = new ColorDrawable(0xb0000000);
		setBackgroundDrawable(dw);
		mListView = (TagFlowLayout) view.findViewById(R.id.id_tagflowlayout);
		mTextView= (TextView) view.findViewById(R.id.txt_state);
		View  mView=  view.findViewById(R.id.view);
		mView.setOnClickListener(mOnClickListener);
		mView.setBackgroundDrawable(dw);
		if (!CheckUtils.isEmpty(mMessage)){
			mTextView.setText(mMessage);
		}

	}

	private View.OnClickListener  mOnClickListener=new View.OnClickListener(){
		@Override
		public void onClick(View v) {
			dismiss();
		}
	};
	private TagFlowLayout.OnSelectListener mOnSelectListener=new TagFlowLayout.OnSelectListener(){
		@Override
		public void onSelected(Set<Integer> selectPosSet) {
			for (Integer  pos:
			selectPosSet) {
				selectPos =pos;
			}
			if (mItemSelectListener != null){
				Object mitem = mAdapter.getItem(selectPos);
				mItemSelectListener.onSureItemClick(mitem);
			}
		}
	};






	
}
